//
//  MessageTimeStatusCell.swift
//  MT
//
//  Created by ooops on 2019/12/15.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

class MessageConfrimCell: MessageTableViewBaseCell {

    override class func reuseIdentity() -> String {
        return "MessageConfrimCellID"
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setupUI() {
        super.setupUI()
        contentView.addSubview(confrimBtn)
        contentView.addSubview(rejectBtn)
    }
    
    override func bindData(_ info: JCVerificationInfo) {
        super.bindData(info)
        switch info.state {
        case JCVerificationType.wait.rawValue, JCVerificationType.receive.rawValue:
            rejectBtn.isHidden = false
            confrimBtn.isHidden = false
        case JCVerificationType.reject.rawValue:
            rejectBtn.isHidden = true
            confrimBtn.isHidden = true
        default:
            break
        }
    }
    
    override func makeConstraint() {
        super.makeConstraint()
        
        confrimBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-16)
        }
        
        rejectBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(confrimBtn.snp.left).offset(-16)
        }
    }
    
    lazy var rejectBtn: UIButton = {
        let btn: UIButton = UIButton(type: .custom)
        btn.setTitle("拒绝", for: .normal)
        btn.setTitleColor(ListViewSubTitleColor, for: .normal)
        btn.titleLabel?.font = PingFangSCRegularFont(12)
        btn.addTarget(self, action: #selector(reject), for: .touchUpInside)
        return btn
    }()
    
    lazy var confrimBtn: UIButton = {
        let btn: UIButton = UIButton(type: .custom)
        btn.setTitle("同意", for: .normal)
        btn.setTitleColor(UIColor(hex: 0xFFC000), for: .normal)
        btn.titleLabel?.font = PingFangSCRegularFont(12)
        btn.addTarget(self, action: #selector(confirm), for: .touchUpInside)
        return btn
    }()
    

    @objc func reject() {
        JMSGFriendManager.rejectInvitation(withUsername: info.username, appKey: info.appkey, reason: "拒绝了你的好友请求") { (result, error) in
            if error == nil {
                self.info.state = JCVerificationType.reject.rawValue
                JCVerificationInfoDB.shareInstance.updateData(self.info)
                self.bindData(self.info)
                NotificationCenter.default.post(name: Notification.Name(rawValue: kUpdateFriendList), object: nil)
            }
        }
    }
    
    @objc func confirm() {
        JMSGFriendManager.acceptInvitation(withUsername: info.username, appKey: info.appkey) { (result, error) in
            if error == nil {
                self.info.state = JCVerificationType.accept.rawValue
                JCVerificationInfoDB.shareInstance.updateData(self.info)
                self.bindData(self.info)
                NotificationCenter.default.post(name: Notification.Name(rawValue: kUpdateFriendList), object: nil)
            }
        }
    }
    
}
